/*
 * @Descripttion: 
 * @Author: junhuafly
 * @Date: 2020-03-05 09:50:02
 * @LastEditors: junhuafly
 * @Email: junhuafly@163.com
 * @LastEditTime: 2020-04-20 14:57:34
 */
// pages/mine/index.js
import {
  userAPI,
  orderAPI
} from './../../api/index'
import {
  returnStatus,
  returnStatusToast,
} from './../../utils/index'

const oftenMenu = [{
    icon: '/pages/images/icon/me_btn_wodezhanghu@2x.png',
    url: '/pages/notwork/index',
    text: '我的钱包'
  }, {
    icon: '/pages/images/icon/me_btn_youhuijuan@2x.png',
    url: '/pages/mine/coupon/index',
    text: '优惠券'
  }, {
    icon: '/pages/images/icon/me_btn_shoucangjia@2x.png',
    url: '/pages/mine/collection/index',
    text: '收藏夹'
  }, {

    icon: '/pages/images/icon/me_btn_xiaoxi@2x.png',
    url: '/pages/mine/msg/index',
    text: '消息'
  }, {

    icon: '/pages/images/icon/me_btn_zhanghubangding@2x.png',
    url: '/pages/notwork/index',
    text: '账户绑定'
  }, {

    icon: '/pages/images/icon/me_btn_dizhiguanli@2x.png',
    url: '/pages/address/index',
    text: '地址管理'
  }, {

    icon: '/pages/images/icon/common_btn_kefu_n@2x.png',
    url: '/pages/notwork/index',
    text: '在线客服'
  }, {

    icon: '/pages/images/icon/me_btn_shezhi@2x.png',
    url: '/pages/mine/setting/index',
    text: '设置'
  }


]
const orderMenu = {
  waitBuyerPayCount: {
    icon: '/pages/images/icon/me_btn_daifukuan@2x.png',
    text: '待付款',
    tab:1
  },
  waitSellerSendCount: {
    icon: '/pages/images/icon/me_btn_daifahuo@2x.png',
    text: '待发货',
    tab:2
  },
  waitBuyerConfirmCount: {
    icon: '/pages/images/icon/me_btn_daishouhuo@2x.png',
    text: '待收货',
    tab:3
  },
  finishedCount: {
    icon: '/pages/images/icon/me_btn_daipingjia@2x.png',
    text: '待评价',
    tab:4
  },
  afterSaleCount: {
    icon: '/pages/images/icon/me_btn_shouhou@2x.png',
    text: '退款/售后',
    tab:5
  }
}
Page({
  /**
   * 页面的初始数据
   */
  data: {
    mineMenus: [{
      text: '笔记',
      name: 'notes',
      url: '/pages/mine/note/index',
      num: 23
    }, {
      text: '关注',
      name: 'follows',
      url: '/pages/mine/attention/index?type=1',
      num: 13,
      dot: false
    }, {
      text: '粉丝',
      name: 'fans',
      url: '/pages/mine/attention/index?type=3',
      num: 0
    }, {
      text: '获赞',
      name: 'likes',
      url: '/pages/mine/attention/index?type=2',
      num: 3
    }],
    orderMenu,
    oftenMenu,
    userInfo: {},
    navbarData:{
      showCapsule: 0,
      back: true
    }
  },
  toAllOrder(){
    wx.navigateTo({
      url: '/pages/mine/order/index?tab=0'
    });
  },
  toDetail({
    currentTarget: {
      dataset: {
        url
      }
    }
  }) {
    wx.navigateTo({
      url,
    });
  },
  toEdit(){
    wx.navigateTo({
      url: '/pages/mine/edit/index',
      success: (result)=>{
        
      },
      fail: ()=>{},
      complete: ()=>{}
    });
  },
  initData(){
    // 获取Init 初始化个人面板
    userAPI.queryCustomer().then(async _ => {
      const res = await returnStatus(_)
      if (res) {
        this.setData({
          'mineMenus[0].num': res['notes'],
          'mineMenus[1].num': res['follows'],
          'mineMenus[2].num': res['fans'],
          'mineMenus[3].num': res['likes'],
        })
      }
    })
    // 获取订单汇总
    orderAPI.queryOrderSummary().then(async res => {
      const _ = await returnStatus(res)
      let orderMenu = this.data.orderMenu
      for (const key in orderMenu) {
        _[key]&&(orderMenu[key].info = _[key])
      }
      console.log(orderMenu);

      this.setData({
        orderMenu
      })
    })
    // 获得缓存中的用户数据
    let userInfo = wx.getStorageSync('userInfo');
    if (!userInfo) {
      userAPI.checkCustomerStatus().then(async _ => {
        const res = await returnStatus(_)
        if (res) {
          wx.setStorage({
            key: 'userInfo',
            data: res,
            success: (result) => {
              userInfo = res
            },
            fail: () => {},
            complete: () => {}
          });

        }
      })
    }
    this.setData({
      userInfo
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var appInst =  getApp();
    appInst.setTitle('个人中心')
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        active: 4
      })
    }
    this.initData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})